Skip to content

feat(cast): Add precision and rounding mode in FloatCast#10086

Draft
patel-vansh wants to merge 2 commits intocodeigniter4:4.8from
patel-vansh:feat/float-cast-precision
Draft

feat(cast): Add precision and rounding mode in FloatCast#10086
patel-vansh wants to merge 2 commits intocodeigniter4:4.8from
patel-vansh:feat/float-cast-precision

Conversation

@patel-vansh
Copy link
Copy Markdown
Contributor

Description
This PR adds ability to set precision as well as rounding mode during Float casting.

So, the float cast can be use as following:

$casts = [
    // ...
    'price' => 'float[2]',
    'price_rounded_even' => 'float[2,even]',
    // ...
];

I haven't updated the user guide yet (that's why the PR is in draft), as before updating the user guide, I wanted others to give their opinion on the implementation.

Also, I had one question, I saw that the casts are defined at two places in system/ folder:

I don't know why there are two different implementation of similar classes, that's why for now, I haven't added the rounding code in Entity/Cast/FloatCast.php, but if its needed, then I'll add (however, Idk if the CPD Github Action will catch it and fail)

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@github-actions github-actions bot added the 4.8 PRs that target the `4.8` branch. label Apr 6, 2026
Copy link
Copy Markdown
Member

@michalsn michalsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We currently need both casters. Entity/Cast handles Entity property casting, while DataCaster/Cast handles Model field casting. Even though Model casting has been the preferred approach since v4.5 and Entity casting may be deprecated in the future, Entity casting is still supported, so new float[...] behavior should be consistent in both places.

Also, the new language key is missing.

Comment on lines +53 to +57
if (isset($modeMap[$modeParam])) {
$mode = $modeMap[$modeParam];
} else {
throw CastException::forInvalidFloatRoundingMode($params[1]);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use match here and throw by default?

$mode = PHP_ROUND_HALF_UP; // Default mode

if (isset($params[1])) {
    $mode = match (strtolower($params[1])) {
        'up'   => PHP_ROUND_HALF_UP,
        'down' => PHP_ROUND_HALF_DOWN,
        'even' => PHP_ROUND_HALF_EVEN,
        'odd'  => PHP_ROUND_HALF_ODD,
        default => throw CastException::forInvalidFloatRoundingMode($params[1]),
    };
}

@neznaika0
Copy link
Copy Markdown
Contributor

Yes, a useful change. Don't forget to update the user guide examples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants